<template>
  <el-row>
    <ImportExcelModal :visible.sync="visible" fileType="chProductInfo" title="导单"
                      downLoadUrl="/static/excel/厂家商品资料导入模板.xlsx">
      <div slot="tips" class="col-xs-12 m--margin-top-10">
        <p class="alert alert-danger font-red text-center">
          由于上传文件转换为订单需要时间处理，上传后文件处理情况请在处理结果中查看，文件处理成功后订单将进入暂存单列表。
        </p>
      </div>
    </ImportExcelModal>

    <ImportResult :visible.sync="resultVisible"
                  url="/ms-common-excelupload/excel/getImportData?tempalateType=chProductInfo"></ImportResult>

    <CreateDialog @refresh="refresh" :visible.sync="addProductModalVisible" :item="row"></CreateDialog>

    <el-col :span="24">
      <Portlet>
        <el-form ref="searchForm" class="el-form-col search-form" :model="search">
          <el-row :gutter="15">

            <el-col :lg="4" :md="6">
              <el-form-item prop="manufacturersNo">
                <el-input v-model="search.manufacturersNo" placeholder="厂家编号"></el-input>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="manufacturersName">
                <el-input v-model="search.manufacturersName" placeholder="厂家名称"></el-input>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="manufacturersUserName">
                <el-input v-model="search.manufacturersUserName" placeholder="厂家联系人"></el-input>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6" class="hidden-lg-only hidden-xl-only">
              <el-form-item>
                <el-row class="search-btns" :gutter="5">
                  <el-col :span="8">
                    <el-button type="primary" :loading="searchLoading" plain native-type="submit"
                               @click.prevent="searchFunc(search)"><span>搜索</span></el-button>
                  </el-col>
                  <el-col :span="8">
                    <el-button type="info" plain @click="isOpen = !isOpen">{{ isOpen ? '收起' : '展开' }}</el-button>
                  </el-col>
                  <el-col :span="8">
                    <el-button @click="resetFields" plain>重置</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="manufacturersUserPhone">
                <el-input v-model="search.manufacturersUserPhone" placeholder="厂家联系电话"></el-input>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="state">
                <el-select v-model="search.state" placeholder="状态">
                  <el-option label="启用" value="1"></el-option>
                  <el-option label="停用" value="0"></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6" class="hidden-md-only hidden-sm-only">
              <el-form-item>
                <el-row class="search-btns" :gutter="5">
                  <el-col :span="12">
                    <el-button type="primary" :loading="searchLoading" plain native-type="submit"
                               @click.prevent="searchFunc(search)"><span>搜索</span></el-button>
                  </el-col>
                  <!--<el-col :span="8">-->
                  <!--<el-button type="info" plain @click="isOpen = !isOpen">{{ isOpen ? '收起' : '展开' }}</el-button>-->
                  <!--</el-col>-->
                  <el-col :span="12">
                    <el-button @click="resetFields" plain>重置</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>
      </Portlet>
    </el-col>

    <el-col :span="24">
      <basic-table
        ref="dataTable"
        url="/ms-warehouse-order/basicData/findManufacturersList"
        :columns="columns"
      >
        <template slot="column" >
          <el-table-column fixed="right" align="center" width="120px" label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="show(scope.row)"
              >
                详情
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="addProduct(scope.row)"
              >
                新增商品
              </el-button>
            </template>
          </el-table-column>
        </template>
        <template slot="buttons">
          <el-button type="primary" @click.prevent="visible = true">导入资料</el-button>
          <el-button type="primary" @click.prevent="resultVisible = true">导入结果</el-button>
        </template>
      </basic-table>
    </el-col>
  </el-row>
</template>
<script>
import {copy} from '@/utils'
import {urlPrefix} from '@/config/index'
import {unixTimestampFilter} from '@/filters'
import {getSubDictListOfParentCode} from '@/utils/selectOptions'
import ImportExcelModal from '@/views/oms/ImportExcelModal'
import StompJs from '@/utils/StompJs'
import {getLoginUser} from '@/utils/ajax'
import moment from 'moment'
import ImportResult from '@/views/business/ImportResult'
import CreateDialog from '@/views/oms/basicData/manufactorPro/CreateDialog'
export default {
  name: 'basicDataManufactorProIndex',
  components: {
    ImportExcelModal,
    ImportResult,
    CreateDialog
  },
  data() {
    return {
      urlPrefix,
      tabIndex: 1,
      search: {
        manufacturersNo: '',
        manufacturersName: '',
        manufacturersUserName: '',
        manufacturersUserPhone: '',
        state: ''
      },
      id: '',
      cargoOwnerId: '',
      row: {},
      addProductModalVisible: false,
      storageTypeCodeArr: [],
      subOrderFinish: '', // 一个消息订阅
      visible: false,
      resultVisible: false,
      isOpen: false,
      searchLoading: false,
      columns: [
        {
          label: '厂家编号',
          data: 'manufacturersNo'
        },
        {
          label: '厂家名称',
          data: 'manufacturersName'
        },
        {label: '厂家联系人', data: 'manufacturersUserName'},
        {label: '厂家联系电话', data: 'manufacturersUserPhone'},
        {
          label: '厂家地址',
          data: 'province+city+district',
          render(data, full) {
            return `${full.province} ${full.city} ${full.district}`
          }
        },
        {label: '详细地址', data: 'address'},
        {label: '操作人', data: 'founder'},
        {
          label: '添加时间',
          data: 'foundDate',
          render(data, type, full, meta) {
            return unixTimestampFilter(data)
          }
        },
        {
          label: '状态',
          data: 'state',
          render(data) {
            return data === 1 ? '启用' : '停用'
          }
        }
      ]
    }
  },
  created() {
    getSubDictListOfParentCode('STORAGETYPE').then(arr => {
      this.storageTypeCodeArr = arr
    })
    let s1 = new StompJs()
    let userId = getLoginUser('id')
    s1.subscribe('/topic/' + userId + 'orderFinish', (message) => {
      this.refresh()
    }).then(sub => {
      this.subOrderFinish = sub
    })
  },
  destroyed() {
    if (this.subOrderFinish) {
      this.subOrderFinish.unsubscribe()
    }
  },
  methods: {
    disabledDate(time) {
      return time.getTime() < moment(this.search.foundStartTime).format('x')
    },
    searchFunc(search) {
      let data = copy(search, true)
      this.searchLoading = true
      this.$refs.dataTable.search(data).finally(_ => {
        this.searchLoading = false
      })
    },
    resetFields(searchForm) {
      this.$refs.searchForm.resetFields()
    },
    refresh() {
      console.log('开始刷新')
      this.searchFunc(this.search)
    },
    show(data) {
      this.$router.push({
        path: '/ss-oms/basic-data/manufactor-pro/index-pro/' + data.id,
        query: {manufacturersName: data.manufacturersName}
      })
    },
    create() {
      this.$router.push({path: '/ss-oms/basic-data/manufactor-pro/create'})
    },
    // 添加产品
    addProduct(data) {
      this.row = data
      this.addProductModalVisible = true
    }
  }
}
</script>
